<?php
namespace App\Logic\SsBasic;

use App\Models\Basics\SysICountry;
use App\Models\Basics\Transport;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\AreaSku;
use App\Models\SsBasic\AreaSkuAvg;
use App\Models\SsBasic\AreaSkuEstimate;
use App\Models\SsBasic\AreaSkuStock;
use App\Models\Stocking\StockingSku;
use App\Models\Shipping\SSku;
use App\Models\Stocking\StockingSkuRatio;
use Exception;
class SyncSS
{
    /**
     * 同步数据
     * 设定：每天运行
     */
    public static function exe()
    {
        $pas = ProductAmazonSku::query()->where('status',1)->select('country_id','psku_id')->distinct()->get();
        $area = [];
        $new = [];
        //先处理数据，将countryId_pSkuId => areaId_pSkuId
        foreach($pas as $_pas){
            if(!array_key_exists($_pas->country_id,$area)){
                $area[$_pas->country_id] = SysICountry::getAreaIdByCountryId($_pas->country_id);
            }
            $value = $area[$_pas->country_id].'_'.$_pas->psku_id;
            if(!in_array($value,$new)){
                $new[] = $value;
            }
        }
        $new = $new ? array_unique($new) : [];
        $count = count($new);
        if($count < 2100){
            report(new Exception("Failed to artisan sync-ss : count(" . $count . ")总数异常",121));
            die(0);
        }
        $old = AreaSku::query()->pluck('area_psku')->toArray();
        //需要删除的数据
        $delete = array_diff($old,$new);
        $country = [];
        foreach($delete as $_delete){
            list($areaId,$pSkuId) = explode('_',$_delete);
            if(!array_key_exists($areaId,$country)){
                $country[$areaId] = SysICountry::getCountryIdsByAreaId($areaId);
            }
            $countryIds = $country[$areaId];
            $count = ProductAmazonSku::query()
                ->where('status',1)
                ->whereIn('country_id',$countryIds)
                ->where('psku_id',$pSkuId)
                ->count();
            if($count > 0)continue;
            AreaSku::query()->where('area_psku',$_delete)->delete();
            StockingSkuRatio::query()->where('area_psku',$_delete)->delete();
            AreaSkuEstimate::query()->where('area_psku',$_delete)->delete();
            AreaSkuAvg::query()->where('area_psku',$_delete)->delete();
            AreaSkuStock::query()->where('area_psku',$_delete)->delete();
        }

        //需要新增的数据
        $add = array_diff($new,$old);
        $skuArr = [];
        $price = [];
        $pCycle = [];
        $transportArr = [];
        foreach($add as $_add){
            $count = AreaSku::query()->where('area_psku',$_add)->count();
            if($count > 0)continue;
            list($areaId,$pSkuId) = explode('_',$_add);
            if(!array_key_exists($pSkuId,$skuArr)){
                $skuArr[$pSkuId] = ProductSku::getModelById($pSkuId);
            }
            $sku = $skuArr[$pSkuId];
            if(!array_key_exists($pSkuId,$price)){
                $price[$pSkuId] = ProductSku::getPuPrice($sku);
            }
            if(!array_key_exists($pSkuId,$pCycle)){
                $pCycle[$pSkuId] = ProductSku::getPuCycle($sku);
            }
            if(!array_key_exists($areaId,$transportArr)){
                $transportArr[$areaId] = Transport::getTransportDay($areaId);
            }
            $transport = $transportArr[$areaId];
            AreaSku::addAreaSku($areaId,$pSkuId,$transport->air_day ?? 0,$transport->sea_day ?? 0,$pCycle[$pSkuId],$price[$pSkuId]);
        }
    }
}
